/**
 * 封装 axios 请求模块
 */
import axios from 'axios';
import store from '../store';
import {Toast} from '@ant-design/react-native';
import {baseURL} from '../config';

// 创建 axios实例
const request = axios.create({
  //  默认设置
  baseURL: baseURL, // 基础API路径
  timeout: 8000, // 响应超时 8000毫秒
});
// 请求拦截器
request.interceptors.request.use(
  config => {
    const storeState = store.getState();
    if (storeState.$userInfo.userInfo) {
      config.headers.token = `${storeState.$userInfo.userInfo.token}`;
    }
    return config;
  },
  error => {
    Toast.fail(String(error) + 'request');
    return Promise.reject(String(error));
  },
);
// 响应拦截器
request.interceptors.response.use(
  response => {
    // 获取相应数据
    const res = response.data;
    return res;
  },
  error => {
    Toast.fail(String(error) + 'response');
    throw new Error(String(error));
  },
);
// 导出请求模块
export default request;
